#include "web.h"
#include <afx.h>
#include <afxinet.h>
#include <iostream>

using namespace std;

BOOL GetFile (CHAR *szUrl, CHAR *szFileName) {
	TCHAR sz[1024];
	CInternetSession session (_T("MyTest agent"), 1,
	INTERNET_OPEN_TYPE_DIRECT);
	CStdioFile* pFile = NULL;
	CHAR   szHead[] = "Accept: */*\r\n\r\n";
	DWORD nRead;
	CFile myFile;
	CFileException fileException;

	if ( !myFile.Open (szFileName, CFile::modeCreate | CFile::modeReadWrite, &fileException ) ) {
		cout << "Can't open file: " << szFileName << " error = " << fileException.m_cause  <<"\n";
		return FALSE;
	}

	try {
		pFile = session.OpenURL (szUrl, 1, INTERNET_FLAG_RELOAD|INTERNET_FLAG_TRANSFER_BINARY,szHead, -1L);
	}
	catch (CInternetException *pEx) {
	cout <<"OpenUrl failed: "<< pEx-> m_dwError << endl;
	return FALSE;
	}
	do {
		nRead = pFile->Read(sz, 1023);
		if (nRead != 0)
		myFile.Write (sz, nRead);
	} while (nRead != 0);
	myFile.Close();
	pFile->Close();
	if (pFile != NULL)
	delete pFile;
	session.Close();
	return TRUE;
}